package com.td.module.cooperative.controller.app.partner;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.cooperative.controller.admin.partner.vo.*;
import com.td.module.cooperative.convert.partner.PartnerConvert;
import com.td.module.cooperative.dal.dataobject.partner.PartnerDO;
import com.td.module.cooperative.service.partner.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户APP - 合作伙伴")
@RestController
@RequestMapping("/cooperative/partner")
@Validated
public class AppPartnerController {

    @Resource
    private PartnerService partnerService;

    @PostMapping("/create")
    @ApiOperation("创建合作伙伴")
    public CommonResult<Long> createPartner(@Valid @RequestBody PartnerCreateReqVO createReqVO) {
        return success(partnerService.createPartner(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新合作伙伴")
    public CommonResult<Boolean> updatePartner(@Valid @RequestBody PartnerUpdateReqVO updateReqVO) {
        partnerService.updatePartner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除合作伙伴")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePartner(@RequestParam("id") Long id) {
        partnerService.deletePartner(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得合作伙伴")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<PartnerRespVO> getPartner(@RequestParam("id") Long id) {
        PartnerDO partner = partnerService.getPartner(id);
        return success(PartnerConvert.INSTANCE.convert(partner));
    }

    @GetMapping("/list")
    @ApiOperation("获得合作伙伴列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<PartnerRespVO>> getPartnerList(@RequestParam("ids") Collection<Long> ids) {
        List<PartnerDO> list = partnerService.getPartnerList(ids);
        return success(PartnerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得合作伙伴分页")
    public CommonResult<PageResult<PartnerRespVO>> getPartnerPage(@Valid PartnerPageReqVO pageVO) {
        PageResult<PartnerDO> pageResult = partnerService.getPartnerPage(pageVO);
        return success(PartnerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/sum-partner-type")
    @ApiOperation("根据partnerType获得合作伙伴统计")
    public CommonResult<List<CountPartnerRespVO>> countPartnerGroupPartnerType() {
        List<CountPartnerRespVO> countPartnerRespVOS = partnerService.countPartnerGroupPartnerType();
        return success(countPartnerRespVOS);
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出合作伙伴 Excel")
    @OperateLog(type = EXPORT)
    public void exportPartnerExcel(@Valid PartnerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PartnerDO> list = partnerService.getPartnerList(exportReqVO);
        // 导出 Excel
        List<PartnerExcelVO> datas = PartnerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "合作伙伴.xls", "数据", PartnerExcelVO.class, datas);
    }

}
